package com.hs.dnms.action.produce;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hs.core.action.BaseAction;
import com.hs.core.db.DBMgr;
import com.hs.core.utils.PageParam;
import com.hs.core.utils.PageUtil;
import com.hs.dnms.Constants;
import com.hs.dnms.model.DnmsBook;
import com.hs.dnms.service.IDnmsBookService;
@Controller  
public class AuditController  extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(AuditController.class);
	private IDnmsBookService bookService;
	@Autowired  
	public void setRoleService(IDnmsBookService bookService) {
		this.bookService = bookService;
	}
	 /**
	  * 待审核数据
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/audit/auditlistFrame.do")   
    public ModelAndView auditlistFrame(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String pid = request.getParameter("id"); 
		 request.setAttribute("id", pid);
		 return new ModelAndView("app/produce/auditlistframe",modelMap);
		 
	 } 
	 /**
	  * 方剂，穴位菜单
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/audit/auditmenu.do")   
    public ModelAndView auditMenu(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 return new ModelAndView("app/produce/auditmenu",modelMap);
		 
	 }
	 /**
	  * 穴位信息
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/audit/auditpoint.do")   
    public ModelAndView auditPoint(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String pgroupid = request.getParameter("pgroupid");
		 String magaid = request.getParameter("magaid");
		 //穴位信息
		 StringBuffer poisql = new StringBuffer();
		 if(null != pgroupid && !"".equals(pgroupid) && !"null".equals(pgroupid)){
			 String tempsql = "select gr.id,gr.pgroupname from dnms_pointgroup gr where gr.id=?";
			 List groupList = DBMgr.getDbUtilInst().queryList(tempsql,new Object[]{pgroupid});
			 List preList = new ArrayList();
			 for(int i=0; i<groupList.size(); i++){
				 Map tempmap = new HashMap();
				 Map onegroup = (Map)groupList.get(i);
				 String gid = String.valueOf(onegroup.get("id"));
				 String gname = (String)onegroup.get("pgroupname");
				 if(null !=gid && !"".equals(gid) && !"null".equals(gid)){
					 tempsql = "select pointname from dnms_point where pgroupid=?";
					 List pointList = DBMgr.getDbUtilInst().queryList(tempsql,new Object[]{gid});
					 String pstr = "";
					 for(int j=0; j<pointList.size(); j++){
						 Map onepoint = (Map)pointList.get(j);
						 pstr = pstr + onepoint.get("pointname") + ",";
					 }
					 if(pstr.endsWith(",")){
						 pstr = pstr.substring(0,pstr.length()-1);
					 }
					 gname = gname + "(" + pstr + ")" ;
					 tempmap.put("pgroupname", gname);
					 preList.add(tempmap);
				 }
				 
			 }
			 modelMap.put("poiList", preList);
		 }else {
			 String tempsql = "select gr.id,gr.pgroupname from dnms_pointgroup gr where  gr.bookid=?";
			 List groupList = DBMgr.getDbUtilInst().queryList(tempsql,new Object[]{magaid});
			 List preList = new ArrayList();
			 for(int i=0; i<groupList.size(); i++){
				 Map tempmap = new HashMap();
				 Map onegroup = (Map)groupList.get(i);
				 String gid = String.valueOf(onegroup.get("id"));
				 String gname = (String)onegroup.get("pgroupname");
				 if(null !=gid && !"".equals(gid) && !"null".equals(gid)){
					 tempsql = "select pointname from dnms_point where pgroupid=?";
					 List pointList = DBMgr.getDbUtilInst().queryList(tempsql,new Object[]{gid});
					 String pstr = "";
					 for(int j=0; j<pointList.size(); j++){
						 Map onepoint = (Map)pointList.get(j);
						 pstr = pstr + onepoint.get("pointname") + ",";
					 }
					 if(pstr.endsWith(",")){
						 pstr = pstr.substring(0,pstr.length()-1);
					 }
					 gname = gname + "(" + pstr + ")" ;
					 tempmap.put("pgroupname", gname);
					 preList.add(tempmap);
				 }
				 
			 }
			 modelMap.put("poiList", preList);
		 }
		 
		 return new ModelAndView("app/produce/auditpoint",modelMap);
		 
	 }
	 /**
	  * 方剂信息
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/audit/auditpre.do")   
    public ModelAndView auditPre(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String pid = request.getParameter("id");
		 String symid = request.getParameter("symid");
		 System.out.println("题录信息主键：" + pid);
		 System.out.println("症候主键：" + symid);
		 if(null != pid && !"".equals(pid) && !"null".equals(pid)){
			//症候信息
			 StringBuffer searchBuffer = new StringBuffer("select pre.prename,pre.fromprename,pre.eatemethod,code.dictname,pre.id "); 
			 searchBuffer.append("  from dnms_pre pre,hs_code code ");
			 searchBuffer.append(" where  pre.pretype=code.dictcode(+) and code.dicttype='");
			 searchBuffer.append(Constants.DISCODE_JIXING);
			 searchBuffer.append("' and pre.bookid=? order by pre.id ");
			 List symList = DBMgr.getDbUtilInst().queryList(searchBuffer.toString(),new Object[]{pid});
			 String tempname = "" ;
			 List preList = new ArrayList();
			 for(int i=0; i<symList.size(); i++){
				 Map tempmap = new HashMap();
				 Map map = (Map)symList.get(i);
				 String prename = (String)map.get("prename");
				 String id = String.valueOf(map.get("id"));
				 if(null !=id && !"".equals(id) && !"null".equals(id)){
					 StringBuffer tempsql = new StringBuffer("select herbname,herbcount,dictname from dnms_herb herb ,hs_code code where herb.countunit=code.dictcode(+) and code.dicttype='");
					 tempsql.append(Constants.DISCODE_JILIANGDANWEI);
					 tempsql.append("' and preid=? order by herb.id");
					 List herbList = DBMgr.getDbUtilInst().queryList(tempsql.toString(),new Object[]{id});
					 String herbStr = "";
					 for(int j=0; j<herbList.size(); j++){
						 Map herb = (Map)herbList.get(j);
						 herbStr = herbStr + herb.get("herbname")+" " + herb.get("herbcount") + herb.get("dictname") + ",";
					 }
					 if(herbStr.endsWith(",")){
						 herbStr = herbStr.substring(0,herbStr.length()-1);
					 }
					
					 prename = prename + "(" + herbStr + ")";
					 tempmap.put("prename", prename);
					 tempmap.put("fromprename", map.get("fromprename"));
					 tempmap.put("pretype", map.get("dictname"));
					 tempmap.put("eatemethod", map.get("eatemethod"));
					 preList.add(tempmap);
				 }
			 }
			 modelMap.put("symList", preList);
		 }else if(null != symid && !"".equals(symid) && !"null".equals(symid)){
			//症候信息
			 StringBuffer searchBuffer = new StringBuffer("select pre.prename,pre.fromprename,pre.eatemethod,code.dictname,pre.id "); 
			 searchBuffer.append("  from dnms_pre pre ,hs_code code");
			 searchBuffer.append(" where pre.pretype=code.dictcode(+) and code.dicttype='");
			 searchBuffer.append(Constants.DISCODE_JIXING);
			 searchBuffer.append("' and pre.symid=? order by pre.id ");
			 List symList = DBMgr.getDbUtilInst().queryList(searchBuffer.toString(),new Object[]{symid});
			 String tempname = "" ;
			 List preList = new ArrayList();
			 for(int i=0; i<symList.size(); i++){
				 Map tempmap = new HashMap();
				 Map map = (Map)symList.get(i);
				 String prename = (String)map.get("prename");
				 String id = String.valueOf(map.get("id"));
				 if(null !=id && !"".equals(id) && !"null".equals(id)){
					 StringBuffer tempsql = new StringBuffer("select herbname,herbcount,dictname from dnms_herb herb ,hs_code code where herb.countunit=code.dictcode(+) and code.dicttype='");
					 tempsql.append(Constants.DISCODE_JILIANGDANWEI);
					 tempsql.append("' and preid=? order by herb.id");
					 List herbList = DBMgr.getDbUtilInst().queryList(tempsql.toString(),new Object[]{id});
					 String herbStr = "";
					 for(int j=0; j<herbList.size(); j++){
						 Map herb = (Map)herbList.get(j);
						 herbStr = herbStr + herb.get("herbname")+" " + herb.get("herbcount") + herb.get("dictname") + ",";
					 }
					 if(herbStr.endsWith(",")){
						 herbStr = herbStr.substring(0,herbStr.length()-1);
					 }
					 prename = prename + "(" + herbStr + ")";
					 tempmap.put("prename", prename);
					 tempmap.put("fromprename", map.get("fromprename"));
					 tempmap.put("pretype", map.get("dictname"));
					 tempmap.put("eatemethod", map.get("eatemethod"));
					 preList.add(tempmap);
				 }
			 }
			 modelMap.put("symList", preList);
		 }
		 return new ModelAndView("app/produce/auditpre",modelMap);
		 
	 }
	 /**
	  * 现代西医病名信息
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/audit/modDisInfo.do")   
    public ModelAndView modDisInfo(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String disid = request.getParameter("disid");
		 String bookid = request.getParameter("bookid");
		 //穴位信息
		 StringBuffer poisql = new StringBuffer();
		 String tempsql = "";
		 List groupList = new ArrayList();
		 if(null != disid && !"".equals(disid) && !"null".equals(disid)){
			 tempsql = "select a.id,a.moddistype,b.disname from DNMS_DIS_ANC_MOD a,DNMS_MODERN_DIS b where a.modid=b.id and a.disid=? order by a.moddistype";
			 groupList = DBMgr.getDbUtilInst().queryList(tempsql,new Object[]{disid});
		 }else {
			 tempsql = "select a.id,a.moddistype,b.disname from DNMS_DIS_ANC_MOD a,DNMS_MODERN_DIS b where a.modid=b.id and a.bookid=? order by a.moddistype";
			 groupList = DBMgr.getDbUtilInst().queryList(tempsql,new Object[]{bookid});
		 }
		 String disnameen = "";
		 String disnamecn = "";
		 for(int i=0; i<groupList.size(); i++){
			 Map onegroup = (Map)groupList.get(i);
			 String tempname = (String)onegroup.get("disname");
			 String moddistype = (String)onegroup.get("moddistype");
			 if("1".equals(moddistype)){
				 disnamecn = disnamecn + tempname + ";";
			 }else if("2".equals(moddistype)){
				 disnameen = disnameen + tempname + ";";
			 }
		 }
		 if(disnameen.endsWith(";")){
			 disnameen = disnameen.substring(0,disnameen.length()-1);
		 }
		 if(disnamecn.endsWith(";")){
			 disnamecn = disnamecn.substring(0,disnamecn.length()-1);
		 }
		 modelMap.put("disnamecn", disnamecn);
		 modelMap.put("disnameen", disnameen);
		 return new ModelAndView("app/produce/auditmoddis",modelMap);
		 
	 }
	 /**
	  * 待审核数据
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/audit/waitAuditlist.do")   
     public ModelAndView waitAuditlist(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         
		 PageParam pageParam = new  PageParam(request,"audit/waitAuditlist.do");
		 PageUtil pageUtil = null;
		 StringBuffer searchBuffer = new StringBuffer(" select a from DnmsBook a where a.state='2'  ");
			String bookname = request.getParameter("bookname");  
			String publishhouse = request.getParameter("publishhouse"); 
			request.setAttribute("bookname", bookname);
			request.setAttribute("publishhouse", publishhouse);
			if(StringUtils.isNotBlank(bookname)){
				searchBuffer.append(" and (a.bookname like '%"+bookname+"%') "); 
			}  
			if(StringUtils.isNotBlank(publishhouse)){
				searchBuffer.append(" and (a.publishhouse like '%"+publishhouse+"%') "); 
			}
			searchBuffer.append(" order by a.id");
			Map data = new HashMap();
			pageParam.setSearchSql(searchBuffer.toString());
			pageUtil = bookService.getList(pageParam);
			data.put("dataList", pageUtil);     
		 return new ModelAndView("app/produce/auditlist",data);
		 
	 } 
	 /**
	  * 待审核数据详细信息
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/audit/auditdataInfo.do")   
     public ModelAndView auditdataInfo(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String pid = request.getParameter("id");
		 System.out.println("题录信息主键：" + pid);
		 //题录信息，文件信息，疾病信息
		 DnmsBook maga = (DnmsBook)this.bookService.findByID(DnmsBook.class, new Long(pid));
		 modelMap.put("obj", maga);
		 //症候信息
		 StringBuffer searchBuffer = new StringBuffer("select sym.id,sym.symptomname "); 
		 searchBuffer.append(" from dnms_sym sym where sym.bookid=? order by sym.id ");
		 List symList = DBMgr.getDbUtilInst().queryList(searchBuffer.toString(),new Object[]{pid});
		 modelMap.put("symList", symList);
		 //穴位信息
		 StringBuffer poiBuffer = new StringBuffer("select gr.id,gr.pgroupname "); 
		 poiBuffer.append(" from dnms_pointgroup gr where gr.bookid=? order by gr.id ");
		 List poiList = DBMgr.getDbUtilInst().queryList(poiBuffer.toString(),new Object[]{pid});
		 modelMap.put("poiList", poiList);
		 return new ModelAndView("app/produce/audit",modelMap);
		 
	 } 
	 /**
	  * 已审核数据
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/audit/completedAuditlist.do")   
     public ModelAndView completedAuditlist(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         
		 PageParam pageParam = new  PageParam(request,"audit/completedAuditlist.do");
		 PageUtil pageUtil = null;
		 StringBuffer searchBuffer = new StringBuffer(" select a from DnmsBook a where state in('3','4')  ");
			String bookname = request.getParameter("bookname");  
			String publishhouse = request.getParameter("publishhouse"); 
			request.setAttribute("bookname", bookname);
			request.setAttribute("publishhouse", publishhouse);
			if(StringUtils.isNotBlank(bookname)){
				searchBuffer.append(" and (a.bookname like '%"+bookname+"%') "); 
			}  
			if(StringUtils.isNotBlank(publishhouse)){
				searchBuffer.append(" and (a.publishhouse like '%"+publishhouse+"%') "); 
			}
			searchBuffer.append(" order by a.id");
			Map data = new HashMap();
			pageParam.setSearchSql(searchBuffer.toString());
			pageUtil = bookService.getList(pageParam);
			data.put("dataList", pageUtil);     
		 return new ModelAndView("app/produce/completedauditlist",data);
		 
	 }
}
